package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.ParamsDao;
import com.baichang.taoshunda.dao.ParamsTypeDao;
import com.baichang.taoshunda.dao.TrendsTypeDao;
import com.baichang.taoshunda.entity.Params;
import com.baichang.taoshunda.entity.ParamsType;
import com.baichang.taoshunda.service.ParamsTypeService;
import com.sun.xml.internal.ws.wsdl.writer.document.ParamType;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/22 0022.
 */

@Service
public class ParamsTypeServiceImpl implements ParamsTypeService {


    @Autowired
    private ParamsTypeDao paramsTypeDao;

    @Autowired
    private ParamsDao paramsDao;
    @Autowired
    private TrendsTypeDao trendsTypeDao;

    /**
     * 展示系统参数列表
     *
     * @return
     */
    @Override
    public Page paramType(int nowPage, int pageSize, int typeId, String paramsName) throws ServiceException {
        return paramsTypeDao.paramsType(nowPage, pageSize, typeId, paramsName);
    }


    /**
     * 添加和修改参数名称
     *
     * @param id
     * @param name
     * @return
     */
    @Override
    public boolean saveOrUpdateParamType(int id, int typeId, String name, int status, String value, String descri, String remark) throws ServiceException {

        if (typeId == 0) {
            if (id == 0) {
                ParamsType paramType = new ParamsType();
                paramType.setName(name);
                paramType.setState(1);
                paramType.setCreated(new Date());
                paramType.setDescri(descri);
                paramsTypeDao.save(paramType);
            } else {
                Map<String, Object> map = new HashedMap();
                map.put("name", name);
                map.put("descri", descri);
                if (paramsTypeDao.update(map, id) > 0) {
                    return true;
                }
            }
        } else {
            if (id == 0) {
                Params params = new Params();
                params.setName(name);
                params.setState(1);
                params.setTypeId(typeId);
                params.setFloorId(0);
                params.setCreated(new Date());
                params.setRemark(remark);
                params.setValue(value);
                paramsDao.save(params);
            } else {
                Map<String, Object> map = new HashedMap();

                if (!("".equals(name))) {
                    map.put("name", name);
                }
                if (!("".equals(remark))) {
                    map.put("remark", remark);
                }

                map.put("value", value);
                if (id == 38) {
                    trendsTypeDao.updateMoney(value);
                }
                if (paramsDao.update(map, id) > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 进行删除
     *
     * @param id
     */

    @Override
    public boolean delete(int id, int typeId) throws ServiceException {

        if (typeId == 0) {
            paramsTypeDao.deleteById(id);
            return true;
        } else {
            paramsDao.deleteById(id);
            return true;
        }
    }
}
